package top.codedance.iotp.loadblance.calgorithm;

import top.codedance.iotp.common.entity.NetworkEntity;
import top.codedance.iotp.common.entity.RegistServerMeta;

import java.util.ArrayList;
import java.util.List;

public class CRandom extends CAdapter {
    private List<RegistServerMeta> rsmMap = new ArrayList<>();
    @Override
    public NetworkEntity execute(List<RegistServerMeta> rsms) {
        for(RegistServerMeta ru : rsms){
            rsmMap.add(ru);
        }
        return doRun();
    }

    private NetworkEntity doRun(){
        java.util.Random random = new java.util.Random();
        random.setSeed(System.currentTimeMillis());
        int i = 0;
        if(rsmMap.size() > 1) {
            i = random.nextInt(rsmMap.size()) % rsmMap.size();
        }
        RegistServerMeta rsm = rsmMap.remove(i);
        NetworkEntity result = new NetworkEntity(rsm.getBindIp(), rsm.getTcpPort());
        return result;
    }
}
